package com.cyz.web.app.mashibing.concurrent;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 两个线程交替输出1A2B3C4D5E6F7G
 */
public class LockSupportClient {

    private static Thread t1;

    private static Thread t2;


    public static void main(String[] args) {
        char[] aI = "1234567".toCharArray();
        char[] aC = "ABCDEFG".toCharArray();

        t1 = new Thread(() -> {

            for (char c : aI) {
                System.out.print(c);
                LockSupport.unpark(t2);//唤醒t2
                LockSupport.park();//当前线程阻塞
            }

        }, "t1");


        t2 = new Thread(() -> {

            for (char c : aC) {
                LockSupport.park(); //当前线程阻塞
                System.out.print(c);
                LockSupport.unpark(t1);//唤醒t1
            }

        }, "t2");

        t1.start();
        t2.start();

    }

}

//通过sync交替输出
class SyncWaitNotify {

    public static void main(String[] args) {
        final Object o = new Object();
        char[] aI = "1234567".toCharArray();
        char[] aC = "ABCDEFG".toCharArray();


        new Thread(() -> {
            synchronized (o) {

                for (char c : aI) {
                    System.out.print(c);
                    try {
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify(); //必须,否则无法停止程序
            }

        }, "t1").start();

        new Thread(() -> {
            synchronized (o) {

                for (char c : aC) {
                    System.out.print(c);
                    try {
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify(); //必须,否则无法停止程序
            }

        }, "t2").start();


    }

}

//通过lock的条件判断交替输出
class LockCondition {

    public static void main(String[] args) {
        char[] aI = "1234567".toCharArray();
        char[] aC = "ABCDEFG".toCharArray();
        Lock lock = new ReentrantLock();
        Condition condition1 = lock.newCondition();     //队列
        Condition condition2 = lock.newCondition();


        new Thread(() -> {
            try {
                lock.lock();
                for (char c : aI) {
                    System.out.print(c);
                    condition2.signal();//唤醒线程
                    condition1.await();
                }

                condition2.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "t1").start();

        new Thread(() -> {
            try {
                lock.lock();
                for (char c : aC) {
                    System.out.print(c);
                    condition1.signal();
                    condition2.await();
                }
                condition1.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "t2").start();


    }


}
